package fw.helpers;

import static fw.Config.BROWSER_CH;
import static fw.Config.BROWSER_FF;
import static fw.Config.BROWSER_IE;
import static fw.Config.DRIVER_CHROME_PATH;
import static fw.Config.DRIVER_IE_PATH;
import static fw.Config.PROXY;

import java.util.Arrays;

import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fw.ApplicationManager;
import fw.utils.WebDriverFactory;

public class WebDriverHelper {
	private final ApplicationManager app;
	private static Logger LOG = LoggerFactory.getLogger(WebDriverHelper.class);

	public WebDriverHelper(ApplicationManager p_app) {
		app = p_app;
	}

	/**
	 * Run a local web browser
	 * 
	 * @param browserName
	 * @param location
	 * @return
	 */
	public WebDriver runLocalBrowser(String p_browser) {
		DesiredCapabilities capabilities = getCapabilities(p_browser);

		if (DRIVER_IE_PATH != null) {
			System.setProperty("webdriver.ie.driver", DRIVER_IE_PATH.getAbsolutePath());
		}
		if (DRIVER_CHROME_PATH != null) {
			System.setProperty("webdriver.chrome.driver", DRIVER_CHROME_PATH.getAbsolutePath());
		}

		return WebDriverFactory.getDriver(capabilities);

	}

	/**
	 * Get desired capabilities
	 * 
	 * @param p_browser
	 * @return
	 */
	@SuppressWarnings("static-access")
	protected DesiredCapabilities getCapabilities(String p_browser) {
		LOG.info("browser: " + p_browser);
		DesiredCapabilities capabilities = null;
		String localProxy = app.proxy().getProxy(PROXY);

		/* Set browser */

		if (p_browser.equals(BROWSER_FF)) {
			capabilities = new DesiredCapabilities().firefox();
			capabilities.setBrowserName("Firefox");

			if (isProxy()) {
				Proxy proxy = new Proxy();
				proxy.setHttpProxy(localProxy);
				capabilities.setCapability("proxy", proxy);
			}
		} else if (p_browser.equals(BROWSER_CH)) {
			capabilities = new DesiredCapabilities().chrome();
			capabilities.setBrowserName("Chrome");

			if (isProxy()) {
				capabilities.setCapability("chrome.switches", Arrays.asList("--proxy-server=http://" + localProxy));
			}
		} else if (p_browser.equals(BROWSER_IE)) {
			capabilities = new DesiredCapabilities().internetExplorer();
			capabilities.setBrowserName("iexplore");

			if (isProxy()) {
				Proxy proxy = new org.openqa.selenium.Proxy();
				proxy.setHttpProxy(localProxy);
				capabilities.setCapability(CapabilityType.PROXY, proxy);
			}
		}

		return capabilities;
	}

	/**
	 * Check for is proxy
	 * 
	 * @return
	 */
	private boolean isProxy() {
		return (!PROXY.equals("def") || PROXY.isEmpty());
	}

	/**
	 * Quit driver
	 */
	public void quit() {
		LOG.info("Driver is clossing...");
		WebDriverFactory.dismissDriver();

	}

	/**
	 * Get Sauce Labs session ID
	 * 
	 * @return
	 */
	public String getSessionId() {
		return (((RemoteWebDriver) app.getDriver()).getSessionId()).toString();
	}

}